package com.haoniu.web.controller.module;

import com.haoniu.common.utils.StringUtils;
import com.haoniu.framework.util.ShiroUtils;
import com.haoniu.module.domain.database.User;
import com.haoniu.module.domain.database.UserInvite;
import com.haoniu.module.domain.database.UserWallet;
import com.haoniu.module.service.UserInviteService;
import com.haoniu.module.service.UserService;
import com.haoniu.module.service.UserWalletService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.haoniu.common.annotation.Log;
import com.haoniu.common.enums.BusinessType;
import com.haoniu.module.domain.database.RoomUser;
import com.haoniu.module.service.RoomUserService;
import com.haoniu.web.core.base.BaseController;
import com.haoniu.framework.web.page.TableDataInfo;
import com.haoniu.common.base.AjaxResult;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
/**
 * 房间用户关联 信息操作处理
 *
 * @author houyijun
 * @date 2019-01-24
 */
@Controller
@RequestMapping("/module/roomUser")
public class RoomUserController extends BaseController
{
    private String prefix = "module/roomUser";

	@Autowired
	private RoomUserService roomUserService;
	@Autowired
	private UserInviteService userInviteService;
	@Resource
	private UserService userService;
	@Resource
	private UserWalletService userWalletService;

	@RequiresPermissions("module:roomUser:view")
	@GetMapping()
	public String roomUser()
	{
	    return prefix + "/roomUser";
	}

	/**
	 * 查询房间用户关联列表
	 */
	@RequiresPermissions("module:roomUser:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(RoomUser roomUserReq) {
		startPage();
        List<RoomUser> list = roomUserService.selectRoomUserList(roomUserReq);
		for (RoomUser roomUser : list) {
			String nickName = roomUser.getUser().getNickName();
			if (StringUtils.isNotEmpty(nickName)) {
				roomUser.setNickName(nickName);
			}
			String phone = roomUser.getUser().getPhone();
			roomUser.setPhone(phone);
			Integer uId = roomUser.getUser().getId();
			roomUser.setuId(uId);
			Integer delFlag = roomUser.getUser().getDelFlag();
			roomUser.setUserDelFlag(delFlag);
			Integer robotType = roomUser.getUser().getRobotType();
			roomUser.setRobotType(robotType);
			Integer userType = roomUser.getUser().getUserType();
			roomUser.setUserType(userType);
			if(null == roomUser.getRoom()){
				roomUser.setName(null);
			}else{
				String roomName = roomUser.getRoom().getName();
				if (StringUtils.isNotEmpty(roomName)) {
					roomUser.setName(roomName);
				}
			}
			roomUser.setuId(roomUser.getUser().getId());
			roomUser.setMoney(roomUser.getUserWallet().getMoney());
			roomUser.setFreezeMoney(roomUser.getUserWallet().getFreezeMoney());
			roomUser.setCreateTime(roomUser.getUser().getCreateTime());
		}
		return getDataTable(list);
	}

	/**
	 * 新增房间用户关联
	 */
	@GetMapping("/add")
	public String add() {
	    return prefix + "/add";
	}

	/**
	 * 添加机器人
	 */
	@RequiresPermissions("module:roomUser:add")
	@Log(title = "添加机器人", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(User user) {

		User user1 = userService.selectUserByPhone(user.getPhone());
		if(null != user1 ){
			return AjaxResult.error("已存在用户");
		}
		User name = userService.selectUserByNickName(user.getNickName());
		if(null  != name){
			return AjaxResult.error("已存在用户");
		}

		User robot = new User();
		robot.setNickName(user.getNickName());
		robot.setPhone(user.getPhone());
		robot.setHeadImg(user.getHeadImg());
		robot.setLeiType(user.getLeiType());
		robot.setUserType(user.getUserType());
		robot.setRobotType(user.getRobotType());
		robot.setCreateTime(new Date());
		user.setCreateBy(ShiroUtils.getLoginName());
		userService.insertUser(robot);

		User user2 = userService.selectUserByPhone(robot.getPhone());
		user2.setInviteCode("000"+user2.getId());
		userService.updateUser(user2);

		UserWallet userWallet = new UserWallet();
		userWallet.setUserId(user2.getId());
		userWallet.setCreateTime(new Date());

		// 新增 分级信息
		UserInvite userInvite = new UserInvite();
		userInvite.setUserId(user2.getId());
		userInviteService.insertUserInvite(userInvite);

		return toAjax(userWalletService.insertUserWallet(userWallet));
	}

	/**
	 * 修改房间用户关联
	 */
	@GetMapping("/edit/{uId}")
	public String edit(@PathVariable("uId") Integer id, ModelMap mmap) {
		//RoomUser roomUser = roomUserService.selectRoomUserById(id)
		User user = userService.selectUserById(id);
		mmap.put("roomUser", user);
	    return prefix + "/edit";
	}

	/**
	 * 修改保存房间用户关联
	 */
	@RequiresPermissions("module:roomUser:edit")
	@Log(title = "房间用户关联", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(RoomUser roomUser) {
        roomUser.setUpdateBy(ShiroUtils.getLoginName());
		return toAjax(roomUserService.updateRoomUser(roomUser));
	}

	/**
	 * 删除房间用户关联
	 */
	@RequiresPermissions("module:roomUser:remove")
	@Log(title = "房间用户关联", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(roomUserService.deleteRoomUserByIds(ids));
	}


	/**
	 * 跳转添加机器人界面
	 */
	@GetMapping("/toAddRobot")
	public String toAddRobot() {

		return prefix + "/add";
	}
}
